#coding=UTF-8 
# from selenium.webdriver.common.action_chains import ActionChains

from chinacQA.TestLink_TestCase import assertBool
from chinacQA.TestLink_TestCase import getConfigure_ENG
from chinacQA.TestLink_TestCase import waitMoment
from chinacQA.Static import testEnvtConf
from chinacQA.TestCase import TestCase
from template.NeoCUUI_ENG import *
from TC_100_NeoCU_Language import STEP_AdminLoginAndChangeLanguage
from TC_106_NeoCUServer import STEP_deleteSnapshot
from TC_106_NeoCUServer import STEP_CheckServerDeleteFunction
from TC_106_NeoCUServer import STEP_deletePriNet
from TC_106_NeoCUServer import STEP_deleteCinder
from TC_106_NeoCUServer import STEP_deleteRubbish
from TC_106_NeoCUServer import logonOut
from TC_103_NeoCUNetwork import STEP_DeleteRouter
# from TC_106_NeoCUServer import deleteProAndUser
from TC_106_NeoCUServer import STEP_deleteTemplate
# from TC_106_NeoCUServer import STEP_deleteSnapshot


def Login(user, pw):
    idusername.text=user
    idpassword.text=pw
    loginBtn.click()

def STEP_UserLogin():
    Login(getConfigure_ENG("User"),getConfigure_ENG("UserNewPassword"))

def STEP_CleanCinderSnapshot():
    StorangeMenu_tab.click()
    Menu_CinderSnapShot.click()
    heads = volumesnapshotsTable.getHeads()
    if("No items to display." in volumesnapshotsTable.getCell(0,0).text):
        return
    rows = len(volumesnapshotsTable.getRows())
    for i in range(rows):
        actioncell=volumesnapshotsTable.getCell(i,0)
        menu_ul = actioncell.find_element("xpath",".//label")
        menu_ul.click()
  
    volumesnapshots__action_delete.click()
    DeleteCinderSnapshot.click()
    
    status = False
    for i in range(100):
        waitMoment(3)
        Menu_CinderSnapShot.click()
        if(len(volumesnapshotsTable.getRows())>0):
            if ("No items to display." in volumesnapshotsTable.getCell(0,0).text.strip()):
                status = True
                break
        else:
            status = True
            break
        if(status):
            break                
    assertBool(status,"3365")

def STEP_DeleteQos():
    waitMoment(3)
    NetworkMenu_tab.click()
    waitMoment(3)
    QosMenu.click()
    heads = qosesTable.getHeads()
    columnPro = heads.index('Project')
    columnName = heads.index('Name')
    for i in range(len(qosesTable.getRows())):
        if(qosesTable.getCell(i,columnPro).text.strip()==getConfigure_ENG("ProjectName") and \
           qosesTable.getCell(i,columnName).text.strip()==getConfigure_ENG("QosNewName")):
            actioncell=qosesTable.getCell(i,0)
            menu_ul = actioncell.find_element("xpath",".//label")
            menu_ul.click()
            
    qoses_action_delete.click()
    DeleteQosButton.click()   
    waitMoment(3)
    status =True
    for i in range(len(qosesTable.getRows())):
        if(qosesTable.getCell(i,columnPro).text.strip()==getConfigure_ENG("ProjectName") and  \
           qosesTable.getCell(i,columnName).text.strip()==getConfigure_ENG("QosNewName") ):
            status = False
    assertBool(status,"3451")

def deleteProAndUser():
    BusinessManager.click()
    ManagerInfo.click()
    tenants_project_filter__q.text = getConfigure_ENG("ProjectName")
    tenants_action_project_filter.click()
    heads = tenants.getHeads()
    columnName = heads.index('Name')
    columnAction = heads.index('Actions')
    for i in range(len(tenants.getRows())):
        if(tenants.getCell(i,columnName)!=None and tenants.getCell(i,columnName).text.strip()==getConfigure_ENG("ProjectName")):
            actioncell=tenants.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//ul/li")
            for button in menubuttons:
                if (button.text.strip()=="Delete QoS"):
                    button.click()
                    deletePro.click()
                    break
                
    status = True  
    if("No items to display." in tenants.getCell(0,0).text.strip()):
        pass    
    else:
        for i in range(len(tenants.getRows())):
            if(tenants.getCell(i,columnName)!=None and tenants.getCell(i,columnName).text.strip()==getConfigure_ENG("ProjectName")):
                status =False
                break
    #assertBool(status,"3175")   
             
    UserManager.click()
    users_user_filter__q.text = getConfigure_ENG("User")
    users_action_user_filter.click()
    heads = userstable.getHeads()
    columnName = heads.index('Login Name')
    columnAction = heads.index('Actions')
    for i in range(len(userstable.getRows())):
        if(userstable.getCell(i,columnName).text.strip()==getConfigure_ENG("User")):
            actioncell=userstable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="Delete"):
                    button.click()
                    deleteuser.click()
                    break
    
    userStatus = True
    if("No items to display." in userstable.getCell(0,0).text.strip()):
        pass
    else:
        for i in range(len(userstable.getRows())):
            if(userstable.getCell(i,columnName).text.strip()==getConfigure_ENG("User")):
                userStatus = False
                break
    #assertBool(userStatus,"3190")

def STEP_Clean():
    logonOut()
    Login(testEnvtConf.AdminName, testEnvtConf.AdminPassword)
    STEP_DeleteQos()
    deleteProAndUser()

def STEP_DeleteCache():
    DataServiceMenu.click()
    waitMoment(3)
    CacheMenu.click()
    if("No items to display." in cachedatabasesTable.getCell(0,0).text):
        return
    
    for i in range(len(cachedatabasesTable.getRows())):
        actioncell=cachedatabasesTable.getCell(i,0)
        menu_ul = actioncell.find_element("xpath",".//div")
        menu_ul.click()
 
    cachedatabases__action_delete.click()
    CacheDelete.click()
    waitMoment(3)
    for i in range(200):
        CacheMenu.click()
        waitMoment(3)
        if("No items to display." in cachedatabasesTable.getCell(0,0).text):
            return

TC_110_NeoCUCleanUp = TestCase("Neocu ENG CleanUp")
TC_110_NeoCUCleanUp.AddStep(STEP_AdminLoginAndChangeLanguage)
TC_110_NeoCUCleanUp.AddStep(STEP_UserLogin)
TC_110_NeoCUCleanUp.AddStep(STEP_deleteSnapshot)
TC_110_NeoCUCleanUp.AddStep(STEP_CleanCinderSnapshot)
TC_110_NeoCUCleanUp.AddStep(STEP_DeleteCache)
TC_110_NeoCUCleanUp.AddStep(STEP_CheckServerDeleteFunction)
TC_110_NeoCUCleanUp.AddStep(STEP_deletePriNet)
#TC_110_NeoCUCleanUp.AddStep(STEP_DeleteRouter)
TC_110_NeoCUCleanUp.AddStep(STEP_deleteCinder)
TC_110_NeoCUCleanUp.AddStep(STEP_deleteRubbish)
TC_110_NeoCUCleanUp.AddStep(STEP_deleteTemplate)
TC_110_NeoCUCleanUp.AddStep(STEP_Clean)

if __name__ == "__main__":
    TC_110_NeoCUCleanUp.run()